﻿using System.Collections.Generic;
using KillBill.Business.Entities;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Administration.IView;
using KillBill.Presentation.Modules.Administration.Presenter;
using System.Linq;

namespace KillBill.Presentation.Modules.Administration.View
{
    public partial class CustomerListView : KillBillWinFormView, ICustomerList
    {
        public CustomerListPresenter Presenter { get; set; }

        private List<Customer> _customerList;

        public CustomerListView()
        {
            InitializeComponent();
            Presenter = new CustomerListPresenter(this);
        }

        public List<Customer> Customers
        {
            set
            {
                var rowIndex = 0;
                DgvCustomers.Rows.Clear();
                foreach (var customer in value)
                {
                    DgvCustomers.Rows.Add();
                    //Person
                    DgvCustomers[0, rowIndex].Value = customer.Person.PersonID;
                    DgvCustomers[1, rowIndex].Value = customer.Person.FirstName;
                    DgvCustomers[2, rowIndex].Value = customer.Person.LastName;
                    DgvCustomers[3, rowIndex].Value = customer.Person.Email;
                    DgvCustomers[4, rowIndex].Value = customer.Person.Address;
                    DgvCustomers[5, rowIndex].Value = customer.Person.Phone;
                    //Customer
                    DgvCustomers[6, rowIndex].Value = customer.CustomerRUC;
                    DgvCustomers[7, rowIndex].Value = customer.Reference;
                    DgvCustomers[8, rowIndex].Value = (customer.IsActive ? Properties.Resources.PersonActive : Properties.Resources.PersonDelete);
                    rowIndex++;
                }
                _customerList = value;
            }
        }

        private void BtnRefresh_Click(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void TxtSearch_TextChanged(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void BtnEdit_Click(object sender, System.EventArgs e)
        {
            var customer = _customerList.Where(c => c.CustomerID == DgvCustomers.SelectedRows[0].Cells[0].Value.ToString()).First();
            var form = new CustomerAddEditView(customer);
            form.ShowDialog();
        }

        private void BtnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            var f = new CustomerAddEditView();
            f.Show();
        }
    }
}
